//
//  ApiCilent.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2018/8/20.
//  Copyright © 2018年 sunke. All rights reserved.
//

#import "ApiCilent.h"
#import "BaseClass.h"

@implementation ApiCilent

static ApiCilent *_sharedClient = nil;
static dispatch_once_t onceToken;

+ (ApiCilent *)sharedClient {
    dispatch_once(&onceToken, ^{
        _sharedClient = [[ApiCilent alloc] init];
        _sharedClient.requestSerializer = [AFJSONRequestSerializer serializer];
        _sharedClient.responseSerializer = [AFJSONResponseSerializer serializer];
    });
    return _sharedClient;
}

-(void)patchData:(NSString *)aPath withParams:(NSDictionary *)params andBlock:(void (^)(id, NSError *))block{
    
    NSLog(@"pathchData->%@",aPath);
    
    [self GET:aPath parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"postJson->%@",[responseObject mj_JSONString]);
        BaseClass * baseClass = [NSObject objectOfClass:@"BaseClass" fromJSON:responseObject];
        int code = baseClass.returnCode;
        if(code == 1){
            block(responseObject,nil);
        }else{
             [NSObject showHudTipStr:[responseObject objectForKey:@"returnMsg"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"pathchData->%@",error);
        block(nil,error);
    }];
}

-(void)getData:(NSString *)aPath withParams:(NSDictionary *)params andBlock:(void (^)(id, NSError *))block{
    
}

-(void)postData:(NSString *)aPath withParams:(NSDictionary *)params andBlock:(void (^)(id, NSError *))block{
    
}

-(void)postJsonData:(NSString *)aPath withParams:(NSDictionary *)params andBlock:(void (^)(id, NSError *))block{
    
    NSLog(@"postJson->%@",[params mj_JSONString]);
    [self POST:aPath parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"postJson->%@",[responseObject mj_JSONString]);
        BaseClass * baseClass = [NSObject objectOfClass:@"BaseClass" fromJSON:responseObject];
        int code = baseClass.returnCode;
        if(code == 1){
            block(responseObject,nil);
        }else{
            [NSObject showHudTipStr:[responseObject objectForKey:@"returnMsg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"postJson->%@",error);
        block(nil,error);
    }];
}


-(void)postPayJsonData:(NSString *)aPath withParams:(NSDictionary *)params andBlock:(void (^)(id, NSError *))block{
    NSLog(@"postJson->%@",[params mj_JSONString]);
    [self POST:aPath parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"postJson->%@",[responseObject mj_JSONString]);
        BaseClass * baseClass = [NSObject objectOfClass:@"BaseClass" fromJSON:responseObject];
        int code = baseClass.returnCode;
        if(code !=0 && code !=100){
            block(responseObject,nil);
        }else if(code == 0){
            [NSObject showHudTipStr:[responseObject objectForKey:@"returnMsg"]];
        }else{
            [NSObject showHudTipStr:@"该笔订单已支付"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"postJson->%@",error);
        block(nil,error);
    }];
}

@end
